package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AccountDiscount;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AccountDiscountService;
import com.tbit.uqbike.webmanager.dao.core.AccountDiscountDao;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:BUGTian
 * @DATE: 2022/9/22
 */
@Service
public class AccountDiscountServiceImpl extends ServiceImpl<AccountDiscountDao, AccountDiscount> implements AccountDiscountService {

    @Autowired
    AccountDiscountDao accountDiscountDao;

    @Override
    public Result insert(AccountDiscount accountDiscount) {
        checkParam(accountDiscount);
        if (count(new QueryWrapper<AccountDiscount>().eq("adAccountId", accountDiscount.getAdAccountId()).eq("configId", accountDiscount.getConfigId())) > 0) {
            return Result.error("认证类型已存在");
        }
        save(accountDiscount);
        return Result.success();
    }

    @Override
    public Result updateAccountDiscount(AccountDiscount accountDiscount) {
        checkParam(accountDiscount);
        update(accountDiscount, new UpdateWrapper<AccountDiscount>().eq("adAccountId", accountDiscount.getAdAccountId()).eq("configId", accountDiscount.getConfigId()));
        return Result.success();
    }

    @Override
    public List<AccountDiscount> queryByAdAccountId(Integer adAccountId) {
        return accountDiscountDao.queryByAdAccountId(adAccountId);
    }

    /**
     * 参数校验
     *
     * @param accountDiscount
     */
    void checkParam(AccountDiscount accountDiscount) {
        Double discount = accountDiscount.getDiscount();
        Assert.notNulls("参数有误", accountDiscount.getAdAccountId(),
                accountDiscount.getAdAccountId(),
                discount,
                accountDiscount.getEnable());
        if (discount <= 0 || discount > 1) {
            throw new BaseException("折扣有误");
        }
    }
}
